﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bordspil.Models;

namespace Bordspil.Controllers
{
    public class HighscoreController : Controller
    {
        private IHighscoreRepository m_repository = null;

        public HighscoreController()
        {
            m_repository = new HighscoreRepository();
        }

        public HighscoreController(IHighscoreRepository rep)
        {
            m_repository = rep;
        }
        // GET: /Highscore/
        public ActionResult Index()
        {
            //birtir highscore haedsta efst notum ekki....en fint ad hafa
            var result = (from high in m_repository.GetAllHighscore()
                          orderby high.UserOrGroupPoints descending
                          select high);

            return View(result);
        }

        public JsonResult GetHighscoreFromGameId(int? gameId)
        {
            //skilar scorei fra leik i json...json er awesome
            var result = (from high in m_repository.GetAllHighscore()
                          where high.GameId == gameId
                          orderby high.UserOrGroupPoints descending
                          select high);
            
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetAllGroupPointsByGameId(int? gameId)
        {
            AppDataContext db = new AppDataContext();
            var users = from m in db.GroupMembers
                        join k in db.Highscore on m.UserProfile.UserName equals k.UserOrGroupName
                        where k.Games.Id == gameId
                        select m;

            return Json(users, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GameHighscore(string gameName)
        {
            // skilar highscori fyrir akvedinn leik
            var result = (from score in m_repository.GetAllHighscore()
                          where gameName == score.Games.GameName
                          select score);

            return View(result);
        }

        public JsonResult GetGroupPoints(string groupName)
        {
            //saekji heildarstig gruppu
            AppDataContext db = new AppDataContext();
            var totalPoints = 0;
            var users = from m in db.GroupMembers
                        join k in db.Highscore on m.UserProfile.UserName equals k.UserOrGroupName
                        where m.Group.GroupName == groupName
                        select k;

            foreach (var i in users)
            {
                //rulla i gegnum allan notendur gruppunar og legg saman stigin teirra
                totalPoints += i.UserOrGroupPoints;
            }

            return Json(totalPoints, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GivePoints(string userName, int gameId, int points)
        {
            //athuga hvort uppfaera eigi stigin
            var giveThis = (from u in m_repository.GetAllHighscore()
                            where u.UserOrGroupName == userName && u.GameId == gameId
                            select u).SingleOrDefault();

            if (giveThis == null)
            {
                //bua til nytt tilvik af Highscore og setja i database
                Highscore h = new Highscore();
                h.GameId = gameId;
                h.IsGroup = false;
                h.UserOrGroupName = userName;
                h.UserOrGroupPoints = points;
                m_repository.UpdateScore(h);
                m_repository.Save();

                return Json(h, JsonRequestBehavior.AllowGet);
            }
            else
            {
                //annars bara uppfaera skorid
                giveThis.UserOrGroupPoints += points;
                m_repository.Save();

                return Json(giveThis, JsonRequestBehavior.AllowGet);
            }
        }

        public JsonResult GetUserPoints(string userName)
        {
            //skilar stigum teirra notanda sem er sendur inn i fallid 
            AppDataContext db = new AppDataContext();
            var result = (from h in db.Highscore
                          join u in db.UserProfile on h.UserOrGroupName equals u.UserName
                          where u.UserName == userName
                          select h);

            return Json(result, JsonRequestBehavior.AllowGet);

        }
    }
}